package com.tcc.fastline.DAO;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.tcc.fastline.DAO.interfaces.IIntegranteFilaDAO;
import com.tcc.fastline.Domain.Fila;
import com.tcc.fastline.Domain.IntegranteFila;
import com.tcc.fastline.Exceptions.ValidationServerException;
import com.tcc.fastline.HttpMethods.ExecuteTask;
import com.tcc.fastline.HttpMethods.HttpDeleteTask;
import com.tcc.fastline.HttpMethods.HttpGetTask;
import com.tcc.fastline.HttpMethods.HttpPostTask;
import com.tcc.fastline.HttpMethods.Urls;

public class IntegranteFilaDAO implements IIntegranteFilaDAO
{
	private Gson gson;
	private Context contexto;

	public IntegranteFilaDAO(Context context) {
		this.contexto = context;
		this.gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create();
	}

	@Override
	public List<IntegranteFila> ObterIntegrantesFila(Integer codigoFila) throws ValidationServerException,
			Exception
	{
		List<IntegranteFila> integrantes = new ArrayList<IntegranteFila>();
		try
		{
			String url = Urls.URL_OBTER_INTEGRANTES_FILA + codigoFila;
			String retorno = ExecuteTask.Execute(new HttpGetTask(url), contexto);

			integrantes = gson.fromJson(retorno,
					new com.google.gson.reflect.TypeToken<List<IntegranteFila>>(){}.getType());
		}
		catch (ValidationServerException e)
		{
			e.printStackTrace();
			throw e;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}

		return integrantes;
	}

	@Override
	public IntegranteFila ObterIntegrante(Integer codigoFila, Integer codigoUsuario)
			throws ValidationServerException, Exception
	{
		IntegranteFila integrante = new IntegranteFila();
		try
		{
			String url = Urls.URL_OBTER_INTEGRANTE + codigoFila + "/" + codigoUsuario;

			String retorno = ExecuteTask.Execute(new HttpGetTask(url), contexto);

			integrante = new IntegranteFila(retorno);

		}
		catch (ValidationServerException e)
		{
			e.printStackTrace();
			throw e;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			Log.e("Erro Exception", e.getMessage());
			throw new RuntimeException(e);
		}

		return integrante;

	}

	@Override
	public void RemoverIntegranteFila(Integer codigoFila, Integer codigoUsuario)
			throws ValidationServerException, Exception
	{
		try
		{
			String url = Urls.URL_REMOVER_INTEGRANTE_FILA + codigoFila.toString() + "/"
					+ codigoUsuario.toString();

			String retorno = ExecuteTask.Execute(new HttpDeleteTask(url), contexto);

		}
		catch (ValidationServerException e)
		{
			throw e;
		}
		catch (Exception e)
		{
			Log.e("Erro Exception", e.getMessage());
			throw new RuntimeException(e);
		}
	}

	@Override
	public void ChamarIntegranteFila(IntegranteFila integrante) throws ValidationServerException, Exception
	{
		try
		{
			String url = Urls.URL_CHAMAR_INTEGRANTE;
			String json = integrante.toString();

			String retorno = ExecuteTask.Execute(new HttpPostTask(url, json), contexto);

		}
		catch (ValidationServerException e)
		{
			throw e;
		}
		catch (Exception e)
		{
			Log.e("Erro Exception", e.getMessage());
			throw new RuntimeException(e);
		}
	}

}
